//------------------------------------------------------------------------------
// MRS Services
//------------------------------------------------------------------------------

using Microsoft.Ccr.Core;
using Microsoft.Dss.Core;
using Microsoft.Dss.Core.Attributes;
using Microsoft.Dss.ServiceModel.Dssp;
using Microsoft.Dss.ServiceModel.DsspServiceBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml;
using mrsservices = MRSServices;
using Microsoft.Dss.Core.DsspHttp;
using System.Net;

namespace MRSServices
{
    /// <summary>
    /// Implementation class for WiiRemote
    /// </summary>
    [DisplayName("WiiRemote")]
    [Description("Provides access to a Nintendo Wii controller connected using Bluetooth.")]
    [Contract(Contract.Identifier)]
    public class WiiRemoteService : DsspServiceBase
    {
        [EmbeddedResource("MRSServices.WiiRemote.xslt")]
        string _transform = null;

        [InitialStatePartner(Optional = true)]
        private WiiRemoteState _state = new WiiRemoteState();

        [ServicePort("/wiiremote", AllowMultipleInstances=true)]
        private WiiRemoteOperations _mainPort = new WiiRemoteOperations();

        private ZWii.WiiMote _remote;
        
        /// <summary>
        /// Default Service Constructor
        /// </summary>
        public WiiRemoteService(DsspServiceCreationPort creationPort) : base(creationPort)
        {
        
        }

        protected override void Shutdown()
        {
            _remote.StopDataStream();
            _remote.Disconnect();

            base.Shutdown();
        }

        /// <summary>
        /// Service Start
        /// </summary>
        protected override void Start()
        {
            if (_state == null)
            {
                _state = new WiiRemoteState();
            }

			//base.Start();

            // Listen on the main port for requests and call the appropriate handler.
            ActivateDsspOperationHandlers();

            // Publish the service to the local service Directory
            DirectoryInsert();

            // start the timer
            Spawn(DateTime.Now, TimerHandler);

            _remote = new ZWii.WiiMote();

            if (!_remote.IsConnected())
            {
                if (_remote.ConnectToDevice(0) && _remote.StartDataStream())
                {
                    
                }
                else
                {
                    _remote.Disconnect();
                }
            }
        }

        void TimerHandler(DateTime signal)
        {
            //_mainPort.Post(new Poll(new PollRequest()));
            //Activate(
            //    Arbiter.Receive(false, TimeoutPort(50), TimerHandler)
            //);
        }

        /// <summary>
        /// Get Handler
        /// </summary>
        /// <param name="get"></param>
        /// <returns></returns>
        [ServiceHandler(ServiceHandlerBehavior.Concurrent)]
        public virtual IEnumerator<ITask> GetHandler(Get get)
        {
            get.ResponsePort.Post(_state);
            yield break;
        }

        [ServiceHandler(ServiceHandlerBehavior.Concurrent)]
        public IEnumerator<ITask> HttpGetHandler(HttpGet httpGet)
        {
            HttpResponseType rsp = new HttpResponseType(HttpStatusCode.OK, _state, _transform);
            httpGet.ResponsePort.Post(rsp);
            yield break;
        }


    }
}
